package LookAndFeel;

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.Image;
import utils.manga.Manga;
import javax.swing.tree.*;
import utils.Servers.SubManga;
import org.jdesktop.swingx.util.OS;
import LookAndFeel.Gui.Imag.ImageLoad;

/*LookAndFeelTest.java
 * Created on 10/09/2010, 07:19:55 PM
 */
/**
 *
 * @author killan
 */
public class LookAndFeelTest extends javax.swing.JFrame {

    //Variables Arbol
    private DefaultMutableTreeNode raiz;
    private List<DefaultMutableTreeNode> mangasTree = new ArrayList<DefaultMutableTreeNode>();
    private DefaultTreeModel modelo;
    //Variables Imagen
    private Image image;
    private int cont = 1;
    private ImageLoad Left, Right;
    //Variables Direcciones
    private String globPath = "C:/imanga/";
    //Variables Archivos
    private File f;
    private BufferedReader br;
    private String linea = "";
    //Variables de Manga
    private List<Manga> manga = new ArrayList<Manga>();

    //Creacion de datos y carpetas de programa
    static {
        File f;
        f = new File("C:/imanga/Manga/");
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File("C:/imanga/Conf/");
        if (!f.exists()) {
            f.mkdirs();
        }
        f = new File("C:/imanga/Conf/series.konf");
        if (!f.exists()) {
            try {
                f.createNewFile();
                SubManga sub = new SubManga();
                sub.getSeries("C:/imanga/");
            } catch (Exception e) {
            }
        }
        f = new File("C:/imanga/Conf/series.favoritos");
        if (!f.exists()) {
            BufferedReader br;
            BufferedWriter bw;
            try {
                f.createNewFile();
                br = new BufferedReader(new FileReader(new File("C:/imanga/Conf/series.konf")));
                bw = new BufferedWriter(new FileWriter(f));
                bw.write(br.readLine());
                bw.newLine();
                bw.write(br.readLine());
                bw.newLine();
                bw.write(br.readLine());
                bw.newLine();
                bw.write(br.readLine());
                bw.newLine();
                bw.write(br.readLine());
                bw.newLine();
                br.close();
                bw.close();
            } catch (Exception ex) {
            }
        }
        Properties prop = new Properties();
        try {
            f = new File("./propi/");
            if (!f.exists()) {
                f.mkdirs();
            }
            f = new File("./propi/pathProperties.properties");
            if (!f.exists()) {
                f.createNewFile();
            }
            String prp = "./propi/pathProperties.properties";
            prop.load(new FileInputStream(f));

            if (!Boolean.getBoolean(prop.getProperty("conf"))) {
                if (OS.isWindows()) {
                    prop.setProperty("so", "windows");
                    prop.setProperty("globPath", "C:/imanga/");
                    prop.setProperty("conf", "true");
                } else if (OS.isLinux()) {
                    prop.setProperty("so", "linux");
                    prop.setProperty("globPath", "/home/imanga/");
                    prop.setProperty("conf", "true");
                }

                prop.store(new FileOutputStream(prp), null);
            }
            System.out.println(prop.size());
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    /** Creates new form LookAndFeelTest */
    public LookAndFeelTest() {
        super("Lector Manga");
        initComponents();

        Left = new ImageLoad();
        Right = new ImageLoad();

        this.jPanel1.add(Left);
        this.jPanel1.add(Right);

        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
        }

        raiz = new DefaultMutableTreeNode("raiz");
        //modelo = new DefaultTreeModel(raiz);
        modelo = new DefaultTreeModel(raiz);

        jTree1 = new JTree(modelo);

        // Construccion de los datos del arbol
        try {
            f = new File(globPath + "Conf/series.favoritos");
            br = new BufferedReader(new FileReader(f));

            while ((linea = br.readLine()) != null) {
                mangasTree.add(new DefaultMutableTreeNode(linea));
                manga.add(new Manga(linea));
            }
            br.close();

            for (int i = 0; i < mangasTree.size(); i++) {
                modelo.insertNodeInto(mangasTree.get(i), raiz, i);
            }
            String path = globPath + "Mangas/One_Piece/" + 606 + "/" + cont + ".jpg";
            ImageLoad.setImageinImagePanel(path, Left);
            cont++;
            String path2 = globPath + "Mangas/One_Piece/" + 606 + "/" + cont + ".jpg";
            ImageLoad.setImageinImagePanel(path2, Right);
        } catch (Exception e) {
        }

        //Escoger los Paneles por defecto
        //jScrollPane1.setViewportView(new ImageLoad());
        jTree1.expandRow(0);
        jTree1.setRootVisible(false);
        DefaultTreeCellRenderer renderer2 = new DefaultTreeCellRenderer();
        renderer2.setOpenIcon(null);
        renderer2.setClosedIcon(null);
        renderer2.setLeafIcon(null);
        jTree1.setCellRenderer(renderer2);
        jScrollPane2.setViewportView(jTree1);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree(raiz);
        jMenuBar1 = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        updateSeriesMen = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        closeMen = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 500));

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jPanel1.setLayout(new java.awt.GridBagLayout());
        jScrollPane1.setViewportView(jPanel1);

        jScrollPane2.setMinimumSize(new java.awt.Dimension(170, 0));
        jScrollPane2.setViewportView(jTree1);

        fileMenu.setText("File");

        updateSeriesMen.setText("Select Folder");
        updateSeriesMen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateSeriesMenActionPerformed(evt);
            }
        });
        fileMenu.add(updateSeriesMen);

        jMenuItem1.setText("jMenuItem1");
        fileMenu.add(jMenuItem1);

        closeMen.setText("Close");
        closeMen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeMenActionPerformed(evt);
            }
        });
        fileMenu.add(closeMen);

        jMenuBar1.add(fileMenu);

        editMenu.setText("Edit");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_RIGHT, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Next");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        editMenu.add(jMenuItem2);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_LEFT, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Prev");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        editMenu.add(jMenuItem3);

        jMenuBar1.add(editMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 632, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeMenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeMenActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_closeMenActionPerformed

    private void updateSeriesMenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateSeriesMenActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)) {
            File selectedFile = fc.getSelectedFile();
            globPath = selectedFile.getPath();
        }
    }//GEN-LAST:event_updateSeriesMenActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        cont++;
        String path = globPath + "Mangas/One_Piece/" + 606 + "/" + cont + ".jpg";
        ImageLoad.setImageinImagePanel(path, Left);
        cont++;
        String path2 = globPath + "Mangas/One_Piece/" + 606 + "/" + cont + ".jpg";
        ImageLoad.setImageinImagePanel(path2, Right);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        cont--;
        String path = globPath + "Mangas/One_Piece/" + 606 + "/" + cont + ".jpg";
        ImageLoad.setImageinImagePanel(path, Left);
        cont--;
        String path2 = globPath + "Mangas/One_Piece/" + 606 + "/" + cont + ".jpg";
        ImageLoad.setImageinImagePanel(path2, Right);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LookAndFeelTest().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem closeMen;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTree jTree1;
    private javax.swing.JMenuItem updateSeriesMen;
    // End of variables declaration//GEN-END:variables
}
